package com.ww.springboot.boot.utils;

import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 该工具类采用抽象类定义，抽象类内部全采用静态方法
 * 通常情况下定义工具类有如下方法：
 * Method I:定义final类，私有化构造函数，类内部全采用静态方法
 * Method II:定义abstract类，类内部全采用静态方法
 * Method III:不建议采用接口+静态方法 或者 非私有化构造函数+静态方法
 * @author wanwei
 */
public class AssertUtil {
	/**
	 * 该类包括:
	 * 判断boolean    (true or false)
	 * 判断object     (is null or not)
	 * 判断string    (has length,has text，contains)
	 * 判断array    (is empty)
	 * 判断collection    (is empty)
	 * 判断map    (is empty)
	 * 判断instanceof
	 * 判断assign
	 * 判断state
	 * 除基本数据类型数组之外基本覆盖全部数据类型
	 * 基本数据类型数组是object对象，但是只能判断is null or not
	 */

	//断言expression必须为true
	public static void isTrue(boolean expression, String message) {
		if (!expression) {
			throw new IllegalArgumentException(message);
		}
	}


	//断言expression必须为true
	public static void isTrue(boolean expression) {
		isTrue(expression, "[Assertion failed] - this expression must be true");
	}

	//断言object对象为空
	public static void isNull(Object object, String message) {
		if (object != null) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言object对象为空
	public static void isNull(Object object) {
		isNull(object, "[Assertion failed] - the object argument must be null");
	}

	//断言object对象不为空
	public static void notNull(Object object, String message) {
		if (object == null) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言object对象不为空
	public static void notNull(Object object) {
		notNull(object, "[Assertion failed] - this argument is required; it must not be null");
	}

	//断言text具有长度，即text != null && text.length()>0
	public static void hasLength(String text, String message) {
		if (!StringUtils.hasLength(text)) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言text具有长度，即text != null && text.length()>0
	public static void hasLength(String text) {
		hasLength(text,
			"[Assertion failed] - this String argument must have length; it must not be null or empty");
	}

	//断言text具有非空白字符
	public static void hasText(String text, String message) {
		if (!StringUtils.hasText(text)) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言text具有非空白字符
	public static void hasText(String text) {
		hasText(text,
			"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
	}

	//断言textToSearch包含substring
	public static void doesNotContain(String textToSearch, String substring, String message) {
		if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
			textToSearch.contains(substring)) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言textToSearch不包含substring
	public static void doesNotContain(String textToSearch, String substring) {
		doesNotContain(textToSearch, substring,
			"[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
	}


	//断言array数组不为空,即array != null && array.length>0
	public static void notEmpty(Object[] array, String message) {
		if (ObjectUtils.isEmpty(array)) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言字符串不为空
	public static void notEmpty(String str, String message) {
		if (ObjectUtils.isEmpty(str)) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言array数组不为空,即array != null && array.length>0
	public static void notEmpty(Object[] array) {
		notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
	}

	//断言array数组不包含空元素
	public static void noNullElements(Object[] array, String message) {
		if (array != null) {
			for (Object element : array) {
				if (element == null) {
					throw new IllegalArgumentException(message);
				}
			}
		}
	}

	//断言array数组不包含空元素
	public static void noNullElements(Object[] array) {
		noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
	}

	//断言collection集合不为空,即collection != null && collection.size>0
	public static void notEmpty(Collection<?> collection, String message) {
		if (CollectionUtils.isEmpty(collection)) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言collection集合不为空,即collection != null && collection.size>0
	public static void notEmpty(Collection<?> collection) {
		notEmpty(collection,
			"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
	}

	//断言map不为空，即map != null && !map.isEmpty()
	public static void notEmpty(Map<?, ?> map, String message) {
		if (CollectionUtils.isEmpty(map)) {
			throw new IllegalArgumentException(message);
		}
	}

	//断言map不为空，即map != null && !map.isEmpty()
	public static void notEmpty(Map<?, ?> map) {
		notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
	}


	//断言obj是clazz的实例
	public static void isInstanceOf(Class<?> clazz, Object obj) {
		isInstanceOf(clazz, obj, "");
	}

	//断言obj是type的实例
	public static void isInstanceOf(Class<?> type, Object obj, String message) {
		notNull(type, "Type to check against must not be null");
		if (!type.isInstance(obj)) {
			throw new IllegalArgumentException(
				(StringUtils.hasLength(message) ? message + " " : "") +
					"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
					"] must be an instance of " + type);
		}
	}

	//断言suoerType是subType的父类
	public static void isAssignable(Class<?> superType, Class<?> subType) {
		isAssignable(superType, subType, "");
	}

	//断言suoerType是subType的父类
	public static void isAssignable(Class<?> superType, Class<?> subType, String message) {
		notNull(superType, "Type to check against must not be null");
		if (subType == null || !superType.isAssignableFrom(subType)) {
			throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
		}
	}


	//断言expression是true,不过抛出IllegalStateException而不是illegalArgumentException
	public static void state(boolean expression, String message) {
		if (!expression) {
			throw new IllegalStateException(message);
		}
	}

	//断言expression是true,不过抛出IllegalStateException而不是illegalArgumentException
	public static void state(boolean expression) {
		state(expression, "[Assertion failed] - this state invariant must be true");
	}

}
